package com.coupon;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.MediaStore;
import android.widget.Toast;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableArray;

import java.io.File;
import android.support.v4.content.FileProvider;

// import org.json.JSONObject;
// import org.json.JSONException;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;

import android.net.Uri;
import java.util.ArrayList;

// uri转path
 import android.database.Cursor;
 import android.provider.MediaStore;

public class uModules extends ReactContextBaseJavaModule {

    private Context context;

    public uModules(ReactApplicationContext reactContext) {
        super(reactContext);
        this.context = reactContext;
    }

    @Override
    public String getName() {
        return "uModules";
    }


    @ReactMethod
    public void getBuildType(Promise p){
        try{
            p.resolve(BuildConfig.buildType);
        } catch (Exception e){
            p.reject(e.getMessage());
        }
    }

    // 获取app版本
    @ReactMethod
    public void getVersionName(Promise p) {
        try {
            PackageInfo info = this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0);
            p.resolve(info.versionName);
        } catch (PackageManager.NameNotFoundException ignored) {
            p.reject("");
        }
    }

    // 获取热更新key
    @ReactMethod
    public void getCodePushKey(Promise p) {
        try{
            p.resolve(BuildConfig.CODEPUSH_KEY);
        } catch (Exception e){
            p.reject(e.getMessage());
        }
    }

    // 分享图片
    @ReactMethod
    public void shareImage(String image, Promise p)
    {
        Intent mulIntent = new Intent(Intent.ACTION_SEND);
        mulIntent.putExtra(Intent.EXTRA_STREAM, image);
        mulIntent.setType("image/png;base64");
        this.context.startActivity(Intent.createChooser(mulIntent, "Share To..."));
        p.resolve(true);
    }

    // 分享多图
    @ReactMethod
    public void shareImages(ReadableArray images, Promise p)
    {
        // ArrayList<String> image2s = images.toArrayList();
        ArrayList<Uri> imageUris = new ArrayList<Uri>();
        for(int k = 0 ; k < images.size(); ++k){
            imageUris.add(Uri.parse(images.getString(k)));
        }
        Intent mulIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        mulIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
        // "image/jpeg"
        mulIntent.setType("image/*");
        // FLAG_GRANT_READ_URI_PERMISSION
        // FLAG_ACTIVITY_NEW_TASK 有的手机还是会弹
        // mulIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Intent chooser = Intent.createChooser(mulIntent, "Share");
        chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // mulIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
        // mulIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | 
        //     Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |
        //     Intent.FLAG_GRANT_READ_URI_PERMISSION);
        // mulIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        // mulIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        // mulIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Intent.createChooser(mulIntent, "Share")
        this.context.startActivity(chooser);
        p.resolve(true);
    }

    @ReactMethod
    public void getContentUri(String filename, Promise p) {
        Uri contentUri = FileProvider.getUriForFile(this.context, BuildConfig.APPLICATION_ID + ".provider", new File(filename));
        p.resolve(contentUri.toString());
    }

    @ReactMethod
    public void getFilenameFromContentUri(String uri, Promise p) {
        // MediaColumns.DATA
        // Images.Media.DATA
        String[] projection = { MediaStore.Images.Media.DATA };
        // managedQuery
        Cursor cursor = this.context.getContentResolver().query(Uri.parse(uri), projection, null, null, null);
        // int column_index = cursor.getColumnIndexOrThrow(projection[0]);  
        cursor.moveToFirst();
        int column_index = cursor.getColumnIndex(projection[0]);  

        uri = cursor.getString(column_index);
        cursor.close();
        p.resolve(uri);
        
    }   
}
